#include "mypushbutton.h"
#include<QPropertyAnimation>

MyPushButton::MyPushButton(QString normalImg,QString pressImg){
    this->normalImgPath=normalImg;
    this->pressImgPath=pressImg;

    QPixmap pix;
    bool res=pix.load(normalImg);
    if(!res){
        qDebug()<<"图片加载失败";
        return;
    }

    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

}

void MyPushButton::zoom(bool isUp){
    //动画对象
    QPropertyAnimation* animation=new QPropertyAnimation(this,"geometry");
    //动画时间
    animation->setDuration(200);
    //开始位置和结束位置
    if(isUp){
        animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
        animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    }
    else{
        animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
        animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    }
    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *e){
    if(this->pressImgPath!=""){
        //切换图像
        QPixmap pix;
        bool res=pix.load(this->pressImgPath);
        if(!res){
            qDebug()<<"图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类继续进行处理，相当于先拦截让函数先实现部分功能
    return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e){
    if(this->normalImgPath!=""){
        //切换图像
        QPixmap pix;
        bool res=pix.load(this->normalImgPath);
        if(!res){
            qDebug()<<"图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类继续进行处理，相当于先拦截让函数先实现部分功能
    return QPushButton::mouseReleaseEvent(e);
}


